﻿Imports System.IO
Public Class MapEditorAddProp
    Inherits System.Windows.Forms.Form
    Implements IMessageFilter
    Private WM_KEYDOWN As Integer = &H100
    Private WM_KEYUP As Integer = &H101
    Public ItemData As String
    Public ItemData_VIRID As String
    Public ItemData_ITEMT As String
    Public ItemData_SHORT As String
    Public ItemData_LONGD As String
    Public ItemData_LOOKD As String
    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
        Dim keyCode As Keys = CType(CInt(m.WParam), Keys) And Keys.KeyCode
        If m.Msg = WM_KEYDOWN And keyCode = Keys.Escape Then
            Me.Close()
            Application.RemoveMessageFilter(Me) : Return True
        End If
        Return False
    End Function 'PreFilterMessage
    Public Function GetFileContents(ByVal FullPath As String, _
           Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try
            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            Return strContents
            objReader.Close()
            objReader.Dispose()
        Catch Ex As Exception
            Return ""
            ErrInfo = Ex.Message
        End Try
    End Function

    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try
            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            objReader.Dispose()
            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message
        End Try
        Return bAns
    End Function
    Dim serr As String
    Dim bans As Boolean
    Private Sub FindProps(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CurSectorPROPLIST.DropDown
        CurSectorPROPLIST.Items.Clear()
        Dim int2 As Integer
        Dim int3 As Integer
        Dim CurNum As Integer = 1
        Dim str1 As String = Replace(Str(CurNum), " ", "")
        ItemData = GetFileContents(Debugger.DirItems, serr)
        int2 = InStr(ItemData, "SHORT#" + str1 + "(")
        While int2 > 0
            For int3 = int2 To Len(ItemData) Step 1
                If Mid(ItemData, int3, 1) = ")" Then
                    CurSectorPROPLIST.Items.Add(Mid(ItemData, int2 + 7 + Len(str1), int3 - int2 - 7 - Len(str1)))
                    CurNum += 1
                    str1 = Replace(Str(CurNum), " ", "")
                    Exit For
                End If
            Next
            int2 = InStr(ItemData, "SHORT#" + str1 + "(")
        End While
    End Sub
    Private Sub AddPropAndHide(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CheckBox1.Checked = True Then
            If MapEditor.MapProps(MapEditor.CurX, MapEditor.CurY) <> "" Then
                MapEditor.MapProps(MapEditor.CurX, MapEditor.CurY) += ",?.[" + TextBox1.Text + "]"
                MapEditor.PropList.Items.Add("?. " + TextBox1.Text)
            Else
                MapEditor.MapProps(MapEditor.CurX, MapEditor.CurY) = "?.[" + TextBox1.Text + "]"
                MapEditor.PropList.Items.Add("?. " + TextBox1.Text)
            End If
        Else
            If MapEditor.MapProps(MapEditor.CurX, MapEditor.CurY) <> "" Then
                MapEditor.MapProps(MapEditor.CurX, MapEditor.CurY) += "," + Replace(Str(CurSectorPROPLIST.SelectedIndex + 1), " ", "") + "." + Mid(CurSectorPROPTypeLIST.Text, 1, 1)
                MapEditor.PropList.Items.Add(Replace(Str(CurSectorPROPLIST.SelectedIndex + 1), " ", "") + ". " + ItemData_SHORT)
            Else
                MapEditor.MapProps(MapEditor.CurX, MapEditor.CurY) = Replace(Str(CurSectorPROPLIST.SelectedIndex + 1), " ", "") + "." + Mid(CurSectorPROPTypeLIST.Text, 1, 1)
                MapEditor.PropList.Items.Add(Replace(Str(CurSectorPROPLIST.SelectedIndex + 1), " ", "") + ". " + ItemData_SHORT)
            End If
        End If
        MapEditor.SND(MapEditor.MapProps(MapEditor.CurX, MapEditor.CurY))
        Me.Close()
    End Sub

    Private Sub closeprop(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub MapEditorAddPropLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(Me)
        FindProps(0, System.EventArgs.Empty)
        CurSectorPROPTypeLIST.Text = CurSectorPROPTypeLIST.Items(0)
    End Sub

    Private Sub Uniquepropchange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            TextBox1.Enabled = True
            CurSectorPROPLIST.Enabled = False
            CurSectorPROPTypeLIST.Enabled = False
        Else
            TextBox1.Enabled = False
            CurSectorPROPLIST.Enabled = True
            CurSectorPROPTypeLIST.Enabled = True
        End If
    End Sub
    Public Sub LoadItem(ByVal ItemVIRID As String)
        ItemData = GetFileContents(Debugger.DirItems, sErr)
        If sErr = "" Then
            Dim tempvar As Integer = InStr(ItemData, "VIRID#" + ItemVIRID + "(")
            Dim tempvar2 As Integer
            If tempvar > 0 Then
                For tempvar2 = InStr(ItemData, "VIRID#" + ItemVIRID + "(") To Len(ItemData) Step 1
                    If Mid(ItemData, tempvar2, 1) = ")" Then
                        ItemData_VIRID = Replace(Mid(ItemData, tempvar + 7 + Len(LTrim(RTrim(ItemVIRID))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemVIRID))))), " ", "")
                        Exit For
                    End If
                Next
                tempvar = InStr(ItemData, "ITEMT#" + ItemVIRID + "(")
                For tempvar2 = InStr(ItemData, "ITEMT#" + ItemVIRID + "(") To Len(ItemData) Step 1
                    If Mid(ItemData, tempvar2, 1) = ")" Then
                        ItemData_ITEMT = Mid(ItemData, tempvar + 7 + Len(LTrim(RTrim(ItemVIRID))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemVIRID)))))
                        Exit For
                    End If
                Next
                tempvar = InStr(ItemData, "SHORT#" + ItemVIRID + "(")
                For tempvar2 = InStr(ItemData, "SHORT#" + ItemVIRID + "(") To Len(ItemData) Step 1
                    If Mid(ItemData, tempvar2, 1) = ")" Then
                        ItemData_SHORT = Mid(ItemData, tempvar + 7 + Len(LTrim(RTrim(ItemVIRID))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemVIRID)))))
                        Exit For
                    End If
                Next
                tempvar = InStr(ItemData, "LONGD#" + ItemVIRID + "(")
                For tempvar2 = InStr(ItemData, "LONGD#" + ItemVIRID + "(") To Len(ItemData) Step 1
                    If Mid(ItemData, tempvar2, 1) = ")" Then
                        ItemData_LONGD = Mid(ItemData, tempvar + 7 + Len(LTrim(RTrim(ItemVIRID))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemVIRID)))))
                        Exit For
                    End If
                Next
                tempvar = InStr(ItemData, "LOOKD#" + ItemVIRID + "(")
                For tempvar2 = InStr(ItemData, "LOOKD#" + ItemVIRID + "(") To Len(ItemData) Step 1
                    If Mid(ItemData, tempvar2, 1) = ")" Then
                        ItemData_LOOKD = Mid(ItemData, tempvar + 7 + Len(LTrim(RTrim(ItemVIRID))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemVIRID)))))
                        Exit For
                    End If
                Next
            End If
        Else
            Debug.WriteLine("Error retrieving file: " & sErr)
        End If
    End Sub
End Class